<?php  
/* 
Template Name: 注册模板
*/  
?>
<?php
require_once(ABSPATH . WPINC . '/registration.php');
global $wpdb, $user_ID;
//检查用户是否登录
if (!$user_ID) {
	if($_POST){
		//验证数据是否全部为空格
		$username = $wpdb->escape($_REQUEST['username']);
		if(empty($username)) {
			echo "用户名不能为空.";
			exit();
		}
		$password = $wpdb->escape($_REQUEST['password']); 
		//验证邮箱格式
		if( empty($password)||strlen($password) < 6  ) {
		echo "错误：密码不能为空，且长度不能小于6！";
		exit();
		}
		$email = $wpdb->escape($_REQUEST['email']);
		//验证邮箱格式
		if( !is_email($email) ) {
			echo "请输入有效的邮箱地址.";
			exit();
		}
		
		$sum= $wpdb->escape($_REQUEST['sum']);
		if($sum==null){
		echo "请填写验证码，谢谢";
		exit();
		}
	
		$sum= $wpdb->escape($_REQUEST['sum']);
		if($sum!=$_POST['num1']+$_POST['num2']){
		echo"抱歉，你输入的验证码不正确，请重新输入";
		exit();
		} 
		
		
		//创建用户
		$status = wp_create_user( $username, $password, $email );
		if ( is_wp_error($status) ) {
			echo $status->get_error_message();
		}else{
			//$from = get_option('admin_email');
			//$headers = '来自: '.$from . "\r\n";
			$subject = "恭喜您！您已经注册成功！";
			$msg = "注册成功.\n你的登录信息\n用户名: $username\n密码: $password";    
			//发送邮件
			wp_mail( $email, $subject, $msg, $headers );
			echo "注册成功，请检查你电子邮件中的登录信息。您也可以使用刚才注册的密码直接登录本站。";
		}
			
		exit();
	}else{
		get_header(); //加载头部问及爱你
		?>

<div class="container">
  <meta name='robots' content='noindex,nofollow' />
  <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/login.css" type="text/css" media="all" />
  <script type="text/javascript" src="<?php bloginfo('template_url'); ?>/js/login.js?v=201303151700"></script>
  <!-- S index Main -->
  <div class="left_main" id="left_side">
    <!--s content_out -->
    <div class="content_out">
      <div class="content">
        <div id="login">
          <div class="registBox">注册
            <div class="loginBoxRel"><a href="<?php bloginfo('url'); ?>/wp-login">登录</a></div>
            <div class="clear"></div>
          </div>
          <!-- s notice -->
          <div class="loginNotice">
            <div class="fontTitle">为什么要注册
              <?php bloginfo('name'); ?>
              ？<br/>
              您可以：</div>
            <ul class="perNotice">
              <li>与网友进行讨论及互动；</li>
              <li>收藏您喜欢的文章和内容；</li>
              <li>积累积分兑换商家优惠券；</li>
              <li>爆料并获取金币奖励；</li>
              <li class="iconNone">……</li>
            </ul>
            <div class="clear"></div>
          </div>
          <!-- e notice -->
          <?php
		//获取是否允许注册
		if(get_option('users_can_register')) {
		?>
          <div id="result" align="center"  style="margin-bottom:15px; font-size:14px; color:red"></div>
          <!-- 为ajax返回结果做准备 -->
          <form name="registerform" id="wp_signup_form" action="" method="post">
            <p>
              <label for="user_login">用户名：</label>
              <input type="text" name="username" id="user_login" class="input" value="" size="20" tabindex="9" autocomplete="off"/>
              <span class="alt_message">只允许用英文字母、数字和下划线</span> </p>
            <p>
              <label for="user_email">电子邮件：</label>
              <input type="email" name="email" id="user_email" class="input" value="" size="25" tabindex="10" autocomplete="off"/>
            </p>
            <p>
              <label>密码：</label>
              <input id="user_pwd1" class="input" type="password" tabindex="21" size="25" value="" name="password" autocomplete="off"/>
              <span class="alt_message">至少6位</span> </p>
            <p>
              <label>用户昵称：</label>
              <input id="user_nicename" class="input" type="text"   tabindex="22" size="25" value="" name="nickname"  autocomplete="off"/>
              <span id="tip" class='alt_message'>只允许用中英文、数字和下划线</span> </p>
            <?php                
                //获取两个随机数, 范围0~9
                $num1=rand(0,9);
                $num2=rand(0,9);
                //最终网页中的具体内容
                echo"<div class='group' style='margin-bottom:15px'>"
                ."  <label style='float:left;'>验证码：$num1 + $num2 =</label> 
				 <input class='input' type='text' name='sum'  size='10px'  size='20' value='' tabindex='1'> "
                ."<input size='40'  type='hidden' name='num1' value='$num1'>"
                ."<input size='40'  type='hidden' name='num2' value='$num2'>"
                ." <span class='alt_message'>请填写计算结果</span>"
                ."</div>"; 
                ?>
            </p>
            <br class="clear" />
            <input type="hidden" name="redirect_to" value="" />
            <p class="submit">
              <input class="button-primary"  name="submit" id="submitbtn"  type="submit" value="提交注册" tabindex="100" />
            </p>
          </form>

        </div>
        <div class="clear"></div>
      </div>
    </div>
  </div>
  
  
            <script type="text/javascript">
		$("#submitbtn").click(function() {
			$('#result').html('<img src="<?php bloginfo('template_url'); ?>/images/loader.gif" alt="<?php the_title(); ?>" class="loader" />').fadeIn();
			var input_data = $('#wp_signup_form').serialize();
			$.ajax({
			type: "POST",
			url:  "<?php echo "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>",
			data: input_data,
			success: function(msg){
				$('.loader').remove();
				$('div#result').html(msg).hide().fadeIn('slow');
			}
			});
			return false;
			
		});
		</script>
  <?php
		}else{
			echo "对不起暂时不开放注册，请以后再试.";
		}
		?>
  <!-- E Main -->
  <?php get_sidebar();?>
  <div class="clear"></div>
</div>
<?php get_footer(); //加载底部文件
	}
}else{
	wp_redirect( home_url() ); exit;
}
?>
